//
//  UIAlertView+RAC.m
//  高仿暴走斗图
//
//  Created by key on 16/3/22.
//  Copyright © 2016年 huangsongyao. All rights reserved.
//

#import "UIAlertView+RAC.h"

@implementation UIAlertView (RAC)

+ (RACSignal *)rac_showAlertWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... {
    
    va_list args;
    va_start(args, otherButtonTitles);
    
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title message:message delegate:delegate cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitles, nil];
    NSString *others = va_arg(args, NSString *);
    
    while (others) {
        if (others.length > 0) {
            [alertView addButtonWithTitle:others];
            others = va_arg(args, NSString *);
        }
    }
    va_end(args);
    
    [alertView show];
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        [[[alertView rac_buttonClickedSignal] deliverOn:[RACScheduler mainThreadScheduler]] subscribeNext:^(id x) {
            [subscriber sendNext:x];
            [subscriber sendCompleted];
        }];
        return nil;
    }];
}

+ (void)showAlertViewNextBlock:(void (^)(id x))nextBlock withTitle:(NSString *)title message:(NSString *)message cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSArray<NSString *> *)otherButtonTitles {
    
    [UIAlertView showAlertViewNextBlock:^(id x) {
        if (nextBlock) {
            nextBlock(x);
        }
    } withTitle:title message:message delegate:nil cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitles];
}

+ (void)showAlertViewNextBlock:(void (^)(id x))nextBlock withTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSArray<NSString *> *)otherButtonTitles {
    
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title message:message delegate:delegate cancelButtonTitle:cancelButtonTitle otherButtonTitles:nil];
    for (NSString *other in otherButtonTitles) {
        [alertView addButtonWithTitle:other];
    }
    [alertView show];
    [[[alertView rac_buttonClickedSignal] deliverOn:[RACScheduler mainThreadScheduler]] subscribeNext:^(id x) {
        nextBlock(x);
    }];
}

+ (RACSignal *)rac_showAlertViewWithTitle:(NSString *)title message:(NSString *)message cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSArray<NSString *> *)otherButtonTitles {
    
    return [UIAlertView rac_showAlertViewWithTitle:title message:message delegate:nil cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitles];
}

+ (RACSignal *)rac_showAlertViewWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSArray<NSString *> *)otherButtonTitles {
    
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title message:message delegate:delegate cancelButtonTitle:cancelButtonTitle otherButtonTitles:nil];
    for (NSString *other in otherButtonTitles) {
        [alertView addButtonWithTitle:other];
    }
    [alertView show];
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        [[[alertView rac_buttonClickedSignal] deliverOn:[RACScheduler mainThreadScheduler]] subscribeNext:^(id x) {
            [subscriber sendNext:x];
            [subscriber sendCompleted];
        }];
        return nil;
    }];
}

@end
